package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.ModuleField;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.service.ModuleFieldService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 *   @description : ModuleField 管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-01-09
 */
@RestController
@Api(value="/field", description="ModuleField 管理")
@RequestMapping("api/field")
public class ModuleFieldController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(ModuleFieldController.class);

    @Autowired
    public ModuleFieldService moduleFieldService;

    /**
     * @description : 获取ModuleField分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:field:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResultMessage page(Integer pageSize, Integer pageNum, String module) {

        pageSize = (pageSize == null) ? 10 : pageSize;
        pageNum = (pageNum == null) ? 1 : pageNum;
        //logger.debug("SQL:"+ew.originalSql());

        Page<ModuleField> page = new Page<ModuleField>(pageNum,pageSize);
        page = moduleFieldService.findModuleFieldList(page,module);

        return ResultMessage.success("获取ModuleField的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有ModuleField记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:field:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultMessage list() {
        return ResultMessage.success("获取所有ModuleField信息",moduleFieldService.findModuleFieldList());
    }

    /**
     * @description : 通过id获取ModuleField
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:field:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取ModuleField信息成功!",moduleFieldService.selectById(id));
    }

    /**
     * @description : 添加ModuleField
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:field:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(ModuleField moduleField){
        moduleField.setCreateUser(getSessionUser().getUserName());
        moduleField.setCreateTime(new Date());
        moduleFieldService.insert(moduleField);
        return ResultMessage.success("添加ModuleField成功!");
    }

    /**
     * @description : 通过id逻辑删除ModuleField
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:field:remove")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    public ResultMessage removeFlag(Long id){
        if(moduleFieldService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑ModuleField删除成功！");
        } else {
            return ResultMessage.fail("逻辑ModuleField删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除ModuleField
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:field:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public ResultMessage remove(long id){
        if(moduleFieldService.deleteById(id)) {
            return ResultMessage.success("物理删除ModuleField成功！");
        } else {
            return ResultMessage.fail("物理删除ModuleField失败！");
        }
    }

    /**
     * @description : 更新ModuleField信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:field:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultMessage update(ModuleField moduleField) {
        moduleField.setUpdateUser(getSessionUser().getUserName());
        moduleField.setUpdateTime(new Date());
        if(moduleFieldService.updateById(moduleField)) {
            return ResultMessage.success("ModuleField信息更新成功!");
        }else{
            return ResultMessage.fail("ModuleField信息更新失败!");
        }
    }
    /**
     * @description : 更新模型信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultMessage updateState(ModuleField moduleField) {
        if(moduleFieldService.updateById(moduleField)) {
            return ResultMessage.success("模型信息更新成功!");
        }else{
            return ResultMessage.fail("模型信息更新失败!");
        }
    }

    /**
    * @description : 清除ModuleField记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
    */
    @RequiresPermissions("dev:module:field:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    public ResultMessage clear(){
        int num = moduleFieldService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有ModuleField记录被清除！");
        }
    }



}
